package com.sunday.android.fangdaifu.bukong.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.common.widgets.UIAlertView;


/**
 * Created by Pual's PC on 2016/10/18.
 */
public class BaseUtil<T> {


    public static String formatPrice2(double totalfee) {
        String s = String.format("%.2f", totalfee);
        if (totalfee < 0) {
            return "0";
        }
        if (String.valueOf(s.charAt(s.length() - 1)).equals("0")) {
            s = s.substring(0, s.length() - 1);
        }
        if (String.valueOf(s.charAt(s.length() - 1)).equals("0")) {
            s = s.substring(0, s.length() - 2);
        }
        return s;
    }

    public static void setEmptyLayout(EmptyLayout emptyLayout, int size, String content) {
        if (size != 0) {
            emptyLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
        } else {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
            emptyLayout.setErrorImag(R.drawable.default_error);
            emptyLayout.setErrorMessage("暂无数据");
            if (!TextUtils.isEmpty(content)) {
                emptyLayout.setErrorMessage(content);
            }
        }
    }

    abstract public static class BaseUtilClickListener implements UIAlertView.ClickListenerInterface {
        private UIAlertView view;

        public BaseUtilClickListener() {

        }

        @Override
        public void doLeft() {
            view.dismiss();
        }

        @Override
        public void doRight() {
            view.dismiss();
            doRightbtn();
        }

        abstract public void doRightbtn();

        public void setView(UIAlertView view) {
            this.view = view;
        }
    }

    public static void showDialog(Context context, String title, BaseUtilClickListener clickListener) {
        final UIAlertView view = new UIAlertView(context, "温馨提示", title, "取消", "确定");
        view.setClicklistener(clickListener);
        clickListener.setView(view);
        view.show();
    }

    public static void openActivity(Context mContext, Class<?> cls) {
        Intent intent = new Intent(mContext, cls);
        mContext.startActivity(intent);
    }

    public static void openActivity(Context mContext, Class<?> cls,int... arg) {
        Intent intent = new Intent(mContext, cls);
        for (int i = 0;i<arg.length;i++){
            intent.putExtra("arg"+i,arg[i]);
        }
        mContext.startActivity(intent);
    }

    public static void openActivity(Context mContext, Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(mContext, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        mContext.startActivity(intent);
    }


    public static Bitmap changeColor(Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int[] colorArray = new int[w * h];
        int n = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                int color = getMixtureWhite(bitmap.getPixel(j, i));
                colorArray[n++] = color;
            }
        }
        return Bitmap.createBitmap(colorArray, w, h, Bitmap.Config.ARGB_8888);
    }

    private static int getMixtureWhite(int color) {
        int alpha = Color.alpha(color);
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);
        return Color.rgb(getSingleMixtureWhite(red, alpha), getSingleMixtureWhite

                        (green, alpha),
                getSingleMixtureWhite(blue, alpha));
    }

    /**
     * 获取单色的混合值
     *
     * @param color
     * @param alpha
     * @return
     */
    private static int getSingleMixtureWhite(int color, int alpha) {
        int newColor = color * alpha / 255 + 255 - alpha;
        return newColor > 255 ? 255 : newColor;
    }

    //数字转换成中文数字，如：2 ---》 二
    static String[] str = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
    static String ss[] = new String[] { "", "十", "百", "千", "万", "十", "百", "千", "亿" };
    public static String intToChiness(int num){
        String s = String.valueOf(num);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            String index = String.valueOf(s.charAt(i));
            sb = sb.append(str[Integer.parseInt(index)]);
        }
        String sss = String.valueOf(sb);
        int i = 0;
        for (int j = sss.length(); j > 0; j--) {
            sb = sb.insert(j, ss[i++]);
        }
        return sb.toString();
    }
}
